{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f7f1d90b41177118",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:04:42.392171Z",
     "start_time": "2025-06-26T02:04:42.387813Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.1203],\n",
       "        [0.5125]], grad_fn=<AddmmBackward0>)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 参数管理\n",
    "import torch\n",
    "from torch import nn\n",
    "\n",
    "net = nn.Sequential(nn.Linear(4,8),nn.ReLU(),nn.Linear(8,1))\n",
    "X = torch.randn(2,4)\n",
    "net(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "47bf37e760e105e9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:04:42.493431Z",
     "start_time": "2025-06-26T02:04:42.454314Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OrderedDict([('weight', tensor([[ 0.1407,  0.2815,  0.4107,  0.2323],\n",
      "        [-0.1673,  0.1332, -0.1582, -0.3983],\n",
      "        [ 0.3380, -0.4784, -0.4643,  0.2651],\n",
      "        [ 0.2951, -0.4160, -0.0853, -0.2216],\n",
      "        [ 0.4300, -0.2097,  0.4877, -0.2176],\n",
      "        [-0.0911,  0.4349, -0.0897, -0.4534],\n",
      "        [ 0.1750, -0.0277,  0.0717,  0.4055],\n",
      "        [ 0.0009, -0.1891, -0.0817, -0.2449]])), ('bias', tensor([ 3.0808e-01,  9.6525e-02,  1.6956e-02,  2.7437e-01,  9.5952e-02,\n",
      "         4.8859e-01, -3.8869e-01, -4.1604e-05]))])\n",
      "OrderedDict([('weight', tensor([[ 0.1407,  0.2815,  0.4107,  0.2323],\n",
      "        [-0.1673,  0.1332, -0.1582, -0.3983],\n",
      "        [ 0.3380, -0.4784, -0.4643,  0.2651],\n",
      "        [ 0.2951, -0.4160, -0.0853, -0.2216],\n",
      "        [ 0.4300, -0.2097,  0.4877, -0.2176],\n",
      "        [-0.0911,  0.4349, -0.0897, -0.4534],\n",
      "        [ 0.1750, -0.0277,  0.0717,  0.4055],\n",
      "        [ 0.0009, -0.1891, -0.0817, -0.2449]])), ('bias', tensor([ 3.0808e-01,  9.6525e-02,  1.6956e-02,  2.7437e-01,  9.5952e-02,\n",
      "         4.8859e-01, -3.8869e-01, -4.1604e-05]))])\n",
      "OrderedDict([('weight', tensor([[-0.1262,  0.3106,  0.3063,  0.2887,  0.1707, -0.0464,  0.3521, -0.1295]])), ('bias', tensor([-0.1646]))])\n"
     ]
    }
   ],
   "source": [
    "print(net[0].state_dict()) #第一层参数信息\n",
    "print(net[0].state_dict()) #激活函数无参数\n",
    "print(net[2].state_dict()) #第三层参数信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "acabd2b6cd7d07a6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:04:42.561597Z",
     "start_time": "2025-06-26T02:04:42.547547Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1: <class 'torch.nn.parameter.Parameter'>\n",
      "2: Parameter containing:\n",
      "tensor([-0.1646], requires_grad=True)\n",
      "3: tensor([-0.1646])\n"
     ]
    }
   ],
   "source": [
    "print(\"1:\",type(net[2].bias))\n",
    "print(\"2:\",net[2].bias)\n",
    "print(\"3:\",net[2].bias.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "28f484d0ef9498c1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:04:42.607770Z",
     "start_time": "2025-06-26T02:04:42.592592Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net[2].weight.grad == None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3bff7df09e95d719",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:04:42.639088Z",
     "start_time": "2025-06-26T02:04:42.624967Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('weight', torch.Size([8, 4])) ('bias', torch.Size([8]))\n",
      "('0.weight', torch.Size([8, 4])) ('0.bias', torch.Size([8])) ('2.weight', torch.Size([1, 8])) ('2.bias', torch.Size([1]))\n"
     ]
    }
   ],
   "source": [
    "print(*[(name, param.shape) for name, param in net[0].named_parameters()]) #第一层参数\n",
    "print(*[(name, param.shape) for name, param in net.named_parameters()]) #所有层参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b59898dee3d94099",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:04:42.670643Z",
     "start_time": "2025-06-26T02:04:42.656071Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-0.1646])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.state_dict()['2.bias'].data #另一种访问参数的方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "328920ca948d21da",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:04:42.701098Z",
     "start_time": "2025-06-26T02:04:42.686389Z"
    }
   },
   "outputs": [],
   "source": [
    "def block1():\n",
    "\treturn nn.Sequential(nn.Linear(4,8),nn.ReLU(),\n",
    "\t\t\t\t\t\t nn.Linear(8,4),nn.ReLU())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c2a298f1cf735993",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:04:57.756162Z",
     "start_time": "2025-06-26T02:04:57.733413Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.0025],\n",
       "        [0.0025]], grad_fn=<AddmmBackward0>)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def block2():\n",
    "\tnet = nn.Sequential()\n",
    "\tfor i in range(4):\n",
    "\t\tnet.add_module(f'block{i}',block1())\n",
    "\treturn net\n",
    "\n",
    "rgnet = nn.Sequential(block2(),nn.Linear(4,1))\n",
    "rgnet(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e0769d1da828a1ec",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:07:35.008195Z",
     "start_time": "2025-06-26T02:07:35.002036Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sequential(\n",
      "  (0): Sequential(\n",
      "    (block0): Sequential(\n",
      "      (0): Linear(in_features=4, out_features=8, bias=True)\n",
      "      (1): ReLU()\n",
      "      (2): Linear(in_features=8, out_features=4, bias=True)\n",
      "      (3): ReLU()\n",
      "    )\n",
      "    (block1): Sequential(\n",
      "      (0): Linear(in_features=4, out_features=8, bias=True)\n",
      "      (1): ReLU()\n",
      "      (2): Linear(in_features=8, out_features=4, bias=True)\n",
      "      (3): ReLU()\n",
      "    )\n",
      "    (block2): Sequential(\n",
      "      (0): Linear(in_features=4, out_features=8, bias=True)\n",
      "      (1): ReLU()\n",
      "      (2): Linear(in_features=8, out_features=4, bias=True)\n",
      "      (3): ReLU()\n",
      "    )\n",
      "    (block3): Sequential(\n",
      "      (0): Linear(in_features=4, out_features=8, bias=True)\n",
      "      (1): ReLU()\n",
      "      (2): Linear(in_features=8, out_features=4, bias=True)\n",
      "      (3): ReLU()\n",
      "    )\n",
      "  )\n",
      "  (1): Linear(in_features=4, out_features=1, bias=True)\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "print(rgnet)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4e7c7e884e973dc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:08:24.247235Z",
     "start_time": "2025-06-26T02:08:24.225115Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.3690, -0.3759,  0.0621,  0.0041,  0.3968, -0.2607,  0.4235, -0.0896])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rgnet[0][1][0].bias.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a9001d74b3616c1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:12:23.524380Z",
     "start_time": "2025-06-26T02:12:23.516925Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-0.0056,  0.0022, -0.0052, -0.0052]),\n",
       " tensor([0., 0., 0., 0., 0., 0., 0., 0.]))"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def init_weight(m):\n",
    "\tif type(m) == nn.Linear:\n",
    "\t\tnn.init.normal_(m.weight, mean=0, std=0.01)\n",
    "\t\tnn.init.zeros_(m.bias)\n",
    "\n",
    "net.apply(init_weight)\n",
    "net[0].weight.data[0], net[0].bias.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "b7724eda2f656baa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:14:14.958487Z",
     "start_time": "2025-06-26T02:14:14.938746Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([1., 1., 1., 1.]), tensor(0.))"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def init_constant(m):\n",
    "\tif type(m) == nn.Linear:\n",
    "\t\tnn.init.constant_(m.weight, 1)\n",
    "\t\tnn.init.zeros_(m.bias)\n",
    "\n",
    "net.apply(init_constant)\n",
    "net[0].weight.data[0],net[0].bias.data[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "6f8b88e7a490b729",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:16:32.414977Z",
     "start_time": "2025-06-26T02:16:32.402134Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([-0.2659,  0.6586, -0.5617,  0.4746])\n",
      "tensor([[42., 42., 42., 42., 42., 42., 42., 42.]])\n"
     ]
    }
   ],
   "source": [
    "def init_xavier(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        nn.init.xavier_uniform_(m.weight)\n",
    "def init_42(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        nn.init.constant_(m.weight, 42)\n",
    "\n",
    "net[0].apply(init_xavier)\n",
    "net[2].apply(init_42)\n",
    "print(net[0].weight.data[0])\n",
    "print(net[2].weight.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "bc3453df35c65c09",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:17:47.510729Z",
     "start_time": "2025-06-26T02:17:47.484146Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Init weight torch.Size([8, 4])\n",
      "Init weight torch.Size([1, 8])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([[-5.6997,  5.3409,  8.0900,  9.2647],\n",
       "        [ 0.0000, -0.0000, -6.1681,  0.0000]], grad_fn=<SliceBackward0>)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def my_init(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        print(\"Init\", *[(name, param.shape)\n",
    "                        for name, param in m.named_parameters()][0])\n",
    "        nn.init.uniform_(m.weight, -10, 10)\n",
    "        m.weight.data *= m.weight.data.abs() >= 5\n",
    "\n",
    "net.apply(my_init) #自定义均值分布，-5到5之间置零\n",
    "net[0].weight[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d3c495d97bada40d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:21:00.700958Z",
     "start_time": "2025-06-26T02:21:00.674264Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([42.0000,  6.3409,  9.0900, 10.2647])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net[0].weight.data[:] += 1 #直接设置参数\n",
    "net[0].weight.data[0, 0] = 42\n",
    "net[0].weight.data[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "27985fc8fe69415d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-26T02:22:38.996483Z",
     "start_time": "2025-06-26T02:22:38.982901Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([True, True, True, True, True, True, True, True])\n",
      "tensor([True, True, True, True, True, True, True, True])\n"
     ]
    }
   ],
   "source": [
    "# 我们需要给共享层一个名称，以便可以引用它的参数\n",
    "shared = nn.Linear(8, 8)\n",
    "net = nn.Sequential(nn.Linear(4, 8), nn.ReLU(),\n",
    "                    shared, nn.ReLU(),\n",
    "                    shared, nn.ReLU(),\n",
    "                    nn.Linear(8, 1))\n",
    "net(X)\n",
    "# 检查参数是否相同\n",
    "print(net[2].weight.data[0] == net[4].weight.data[0])\n",
    "net[2].weight.data[0, 0] = 100\n",
    "# 确保它们实际上是同一个对象，而不只是有相同的值\n",
    "print(net[2].weight.data[0] == net[4].weight.data[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "250c8de8aa3b8847",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
